using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Nojin.Deployment;

namespace DeploymentManifestBuilder
{
    public partial class MainForm : Form
    {
        List<DownloadEntry> entries = new List<DownloadEntry>();
        public MainForm()
        {
            InitializeComponent();

            listBoxEntries.DataSource = entries;
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            EntryForm addEntryForm = new EntryForm();
            addEntryForm.ShowDialog();
            if (addEntryForm.DialogResult == DialogResult.OK)
            {
                DownloadEntry entry = new DownloadEntry();
                entry.Uri = addEntryForm.FileName;
                entry.InstallAction = addEntryForm.InstallAction;
                entries.Add(entry);
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            DeploymentManifest manifest = new DeploymentManifest();
            string sourceBase = textBoxSourceBase.Text;
            string hostBase = textBoxHostBase.Text;
            if (hostBase[hostBase.Length - 1] != '/')
                hostBase += '/';
            foreach (DownloadEntry entry in entries)
            {
                DownloadEntry toAddEntry = new DownloadEntry();
                toAddEntry.InstallAction = entry.InstallAction;

                string fileRelativePath = entry.Uri.Substring(sourceBase.Length);
                int fileNameBegin = fileRelativePath.LastIndexOf('/');
                if (fileNameBegin > 0)
                {
                    toAddEntry.TargetPath = fileRelativePath.Substring(0, fileNameBegin - 1);                    
                }
                toAddEntry.Uri = hostBase + fileRelativePath;
                manifest.Downloads.Add(toAddEntry);
            }

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                DeploymentManifest.Save(saveFileDialog.FileName, manifest);
            }
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            DownloadEntry selectedEntry = listBoxEntries.SelectedItem as DownloadEntry;
            if (selectedEntry != null)
            {
                EntryForm editEntryForm = new EntryForm();
                editEntryForm.FileName = selectedEntry.Uri;
                editEntryForm.InstallAction = selectedEntry.InstallAction;
                editEntryForm.ShowDialog();
                if (editEntryForm.DialogResult == DialogResult.OK)
                {
                    selectedEntry.Uri = editEntryForm.FileName;
                    selectedEntry.InstallAction = editEntryForm.InstallAction;
                }
            }
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {

        }
    }
}